select  ft.ftz_num                                                                       NRO_FATTURAZIONE,   
        ft.FAT_ANNO                                                                       ANNO_FATTURA,       
        ft.FAT_NUM                                                                        NRO_FATTURA,
        ft.tfat_id                                                                        ID_FATTURA,
        ft.FAT_FLTIPO                                                                     TIPO_FATTURA,
        ft.regiva_id                                                                      ID_REGISTRO_IVA,
        ft.regivapro_id                                                                   ID_PROTOCOLLO_IVA,
        ft.regivapro_label                                                                LABEL_PROTOCOLLO_IVA,
        ft.ragf_id                                                                        ID_RAGGRUPPAMENTO,
        trunc( ft.FAT_DT)                                                               DATA_FATTURA,
        rf.sog_id                                                                         CODICE_CLIENTE,
        NVL( sog.sog_codfis, sog.SOG_PARIVA )                                             CODICE_FISCALE_PARTITA_IVA,
        ft.FAT_DECRAGSOC                                                                  RAGIONE_SOCIALE_CLIENTE,
        cuf.con_anno                                                                      ANNO_CONTRATTO,
        cuf.con_id                                                                        CODICE_CONTRATTO,
        cont.con_oldconid                                                                 VECCHIO_CODICE_CONTRATTO,
        cuf.cuf_id                                                                        CUF_ID,
        ( select t.tcon_des
            from tcontratto t
            where cont.tcon_id = t.tcon_id)                                               TIPO_CONTRATTO,
        TRUNC( cont.ver_dtini )                                                           DATA_INIZIO_CONTRATTO,
        TRUNC( cont.ver_dtfine )                                                          DATA_FINE_CONTRATTO,
        TRUNC( cont.con_dtiniges )                                                        DATA_INIZIO_GESTIONE,
        cuf.att_id                                                                        CODICE_ATTACCO,
        ( select max(att.att_idext)
            from  attacco  att
            where att.az_id   = ft.az_id
              and att.att_id = cuf.att_id )                                                PDR,
        ( select max(vatt.vcatt_val)
            from  v_valcxatt  vatt
            where cuf.az_id   = vatt.az_id
              and vatt.sta_idsys = 2 
              and cuf.att_id = vatt.att_id
              and vatt.catt_id = 4
              and vatt.ver_dtini <= fsez.sfat_dtfinevalcon
              and vatt.ver_dtfine >= fsez.sfat_dtinivalcon  
              )                                                                     TIPO_FORNITURA_EE,
        ( select max(vatt.vcatt_val)
            from  v_valcxatt  vatt
            where cuf.az_id   = vatt.az_id
              and vatt.sta_idsys = 2 
              and cuf.att_id = vatt.att_id
              and vatt.catt_id = 3
              and vatt.ver_dtini <= fsez.sfat_dtfinevalcon
              and vatt.ver_dtfine >= fsez.sfat_dtinivalcon 
               )                                                                       POTENZA_IMPEGNATA,
        ( select max(vatt.vcatt_val)
            from  v_valcxatt  vatt
            where cuf.az_id   = vatt.az_id
              and vatt.sta_idsys = 2 
              and cuf.att_id = vatt.att_id
              and vatt.catt_id = 2
               and vatt.ver_dtini <= fsez.sfat_dtfinevalcon
              and vatt.ver_dtfine >= fsez.sfat_dtinivalcon  
              )                                                                     POTENZA_DISPONIBILE,
        ( select max(vatt.vcatt_val)
            from  v_valcxatt  vatt
            where cuf.az_id   = vatt.az_id
              and vatt.sta_idsys = 2 
              and cuf.att_id = vatt.att_id
              and vatt.catt_id = 6
              and vatt.ver_dtini <= fsez.sfat_dtfinevalcon
              and vatt.ver_dtfine >= fsez.sfat_dtinivalcon 
               )                                                                       FASI,
        ( select max(vatt.vcatt_val)
            from  v_valcxatt  vatt
            where cuf.az_id   = vatt.az_id
              and vatt.sta_idsys = 2 
              and cuf.att_id = vatt.att_id
              and vatt.catt_id = 7
               and vatt.ver_dtini <= fsez.sfat_dtfinevalcon
              and vatt.ver_dtfine >= fsez.sfat_dtinivalcon  
              )                                                                     TENSIONE_VOLT,                    
        ( select max(trunc( cufrip.ver_dtini)) 
            from  v_conubiperrip  cufrip,
                  fatturasrip   fsrip
            where fsrip.az_id   = ft.az_id
              and fsrip.sfat_id = fsez.sfat_id
              and cufrip.az_id  = ft.az_id
--              and cufrip.sta_idsys = 2 
              and cufrip.cufpr_num = fsrip.cufpr_num )                                    DATA_INIZIO_PERIODO_FATTURATO,
        ( select max(trunc( cufrip.ver_dtfine) )
            from  v_conubiperrip  cufrip,
                  fatturasrip   fsrip
            where fsrip.az_id   = ft.az_id
              and fsrip.sfat_id = fsez.sfat_id
              and cufrip.az_id  = ft.az_id
--              and cufrip.sta_idsys = 2 
              and cufrip.cufpr_num = fsrip.cufpr_num )                                    DATA_FINE_PERIODO_FATTURATO,
        ( select max(( trunc( cufrip.ver_dtfine ) - trunc( cufrip.ver_dtini )) + 1)
            from  v_conubiperrip  cufrip,
                  fatturasrip   fsrip
            where fsrip.az_id   = ft.az_id
              and fsrip.sfat_id = fsez.sfat_id
              and cufrip.az_id  = ft.az_id
--              and cufrip.sta_idsys = 2
              and cufrip.cufpr_num = fsrip.cufpr_num )                                    GG_FATTURATI,
      ( select min (trunc(con.ver_dtini )) 
            from  fatturaslc   f,
                     convalsf sf,
                     conval con 
            where f.az_id   = ft.az_id
              and f.sfat_id = fsez.sfat_id
              and sf.az_id = f.az_id  
             and f.conssf_id = sf.conssf_id
              and sf.az_id  = con.az_id
              and sf.cval_vernum = con.ver_num 
             --and cons.sta_idsys in (3,10) 
              and f.sfatlc_fltipoope = '0')                                            DATA_INIZIO_CONSUMO_FATTURATO,
             
        ( select max ( trunc( con.ver_dtfine)  )
         from  fatturaslc   f,
                     convalsf sf,
                     conval con 
            where f.az_id   = ft.az_id
              and f.sfat_id = fsez.sfat_id
              and sf.az_id = f.az_id  
             and f.conssf_id = sf.conssf_id
              and sf.az_id  = con.az_id
              and sf.cval_vernum = con.ver_num 
             --and cons.sta_idsys in (3,10) 
              and f.sfatlc_fltipoope = '0')                                            DATA_FINE_CONSUMO_FATTURATO,         
        cuf.DECODE_INDID                                                                  INDIRIZZO_SITO_FORNITURA,
        cuf.COM_DES                                                                       COMUNE_FORNITURA,
        fsez.sfat_id,
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        ATT_FU_REALE_FATTURAZIONE,
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        ATT_F1_REALE_FATTURAZIONE,
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        ATT_F2_REALE_FATTURAZIONE,
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        ATT_F3_REALE_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        ATT_FU_PRESUNTA_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        ATT_F1_PRESUNTA_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        ATT_F2_PRESUNTA_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        ATT_F3_PRESUNTA_FATTURAZIONE,

      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        ATT_FU_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        ATT_F1_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        ATT_F2_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        ATT_F3_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        ATT_FU_PRESUNTA_RETTIFICA,                          
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        ATT_F1_PRESUNTA_RETTIFICA,                          
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        ATT_F2_PRESUNTA_RETTIFICA,                          
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'ATT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        ATT_F3_PRESUNTA_RETTIFICA,                          
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        REA_FU_REALE_FATTURAZIONE,
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        REA_F1_REALE_FATTURAZIONE,
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        REA_F2_REALE_FATTURAZIONE,
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        REA_F3_REALE_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        REA_FU_PRESUNTA_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        REA_F1_PRESUNTA_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        REA_F2_PRESUNTA_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        REA_F3_PRESUNTA_FATTURAZIONE,

      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        REA_FU_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        REA_F1_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        REA_F2_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        REA_F3_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        REA_FU_PRESUNTA_RETTIFICA,                          
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        REA_F1_PRESUNTA_RETTIFICA,                          
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        REA_F2_PRESUNTA_RETTIFICA,                          
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'REA'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        REA_F3_PRESUNTA_RETTIFICA,                          
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        POT_FU_REALE_FATTURAZIONE,
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        POT_F1_REALE_FATTURAZIONE,
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        POT_F2_REALE_FATTURAZIONE,
         ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope = '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon = '1' )                                        POT_F3_REALE_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        POT_FU_PRESUNTA_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        POT_F1_PRESUNTA_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        POT_F2_PRESUNTA_FATTURAZIONE,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '0'      -- Fatturazione
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        POT_F3_PRESUNTA_FATTURAZIONE,

      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        POT_FU_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        POT_F1_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        POT_F2_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '1' )                                        POT_F3_REALE_RETTIFICA,
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'FU'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        POT_FU_PRESUNTA_RETTIFICA,                          
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F1'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        POT_F1_PRESUNTA_RETTIFICA,                          
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F2'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        POT_F2_PRESUNTA_RETTIFICA,                          
      ( select sum(c.cval_val)
          from fatturas s,
                   fatturaslc sc,
                   convalsf sf,
                  v_conval c,   
                  consumo  co
          where s.sfat_id = fsez.sfat_id
          and s.az_id = fsez.az_id
          and s.az_id = sc.az_id
          and s.sfat_id = sc.sfat_id
          and sc.az_id = sf.az_id
          and sc.conssf_id = sf.conssf_id
          and sc.sfatlc_fltipoope || ''= '2'      -- Rettifica 
          and sf.az_id = c.az_id 
          and sf.cval_vernum = c.ver_num
          and c.fuco_id = 'POT'
          and c.faco_id = 'F3'
          and co.az_id = c.az_id
          and co.cons_id = c.cons_id 
          and co.cons_fltipocon || ''= '2' )                                        POT_F3_PRESUNTA_RETTIFICA,                          
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = fsez.az_id
           and cu.cuf_id = fsez.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= fsez.sfat_dtfinevalcon
           and cu.ver_dtfine >= fsez.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= fsez.sfat_dtfinevalcon
                                 and ver_dtfine >= fsez.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 7
           and o.comser_id = 16
            )                                                              OPZIONE_VENDITA,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = fsez.az_id
           and cu.cuf_id = fsez.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= fsez.sfat_dtfinevalcon
           and cu.ver_dtfine >= fsez.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= fsez.sfat_dtfinevalcon
                                 and ver_dtfine >= fsez.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 7
           and o.comser_id = 17
            )                                                              OPZIONE_ERARIALE,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = fsez.az_id
           and cu.cuf_id = fsez.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= fsez.sfat_dtfinevalcon
           and cu.ver_dtfine >= fsez.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= fsez.sfat_dtfinevalcon
                                 and ver_dtfine >= fsez.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 7
           and o.comser_id = 18
            )                                                              OPZIONE_ADDIZIONALE_PROV,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = fsez.az_id
           and cu.cuf_id = fsez.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= fsez.sfat_dtfinevalcon
           and cu.ver_dtfine >= fsez.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= fsez.sfat_dtfinevalcon
                                 and ver_dtfine >= fsez.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 7
           and o.comser_id = 19
            )                                                             OPZIONE_ADDIZIONALE_COMUN,           
                ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = fsez.az_id
           and cu.cuf_id = fsez.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= fsez.sfat_dtfinevalcon
           and cu.ver_dtfine >= fsez.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= fsez.sfat_dtfinevalcon
                                 and ver_dtfine >= fsez.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 8
           and o.comser_id = 21
            )                                                              OPZIONE_TRASPORTO,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = fsez.az_id
           and cu.cuf_id = fsez.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= fsez.sfat_dtfinevalcon
           and cu.ver_dtfine >= fsez.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= fsez.sfat_dtfinevalcon
                                 and ver_dtfine >= fsez.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 7
           and o.comser_id = 22
            )                                                               OPZIONE_TRASMISSIONE,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = fsez.az_id
           and cu.cuf_id = fsez.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= fsez.sfat_dtfinevalcon
           and cu.ver_dtfine >= fsez.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= fsez.sfat_dtfinevalcon
                                 and ver_dtfine >= fsez.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 8
           and o.comser_id = 23
            )                                                             OPZIONE_COMPONENTI_A_UC,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = fsez.az_id
           and cu.cuf_id = fsez.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= fsez.sfat_dtfinevalcon
           and cu.ver_dtfine >= fsez.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= fsez.sfat_dtfinevalcon
                                 and ver_dtfine >= fsez.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 8
           and o.comser_id = 23
            )                                                              OPZIONE_PENALE_REATTIVA,       
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = fsez.az_id
           and cu.cuf_id = fsez.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= fsez.sfat_dtfinevalcon
           and cu.ver_dtfine >= fsez.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= fsez.sfat_dtfinevalcon
                                 and ver_dtfine >= fsez.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 8
           and o.comser_id = 25
            )                                                              OPZIONE_MISURA,
        ( select o.ot_dessta
           from   v_cufot  cu,
                  ot       o
           where cu.az_id = fsez.az_id
           and cu.cuf_id = fsez.cuf_id
           and cu.sta_idsys = 2
           and cu.ver_dtini <= fsez.sfat_dtfinevalcon
           and cu.ver_dtfine >= fsez.sfat_dtinivalcon
           and cu.ver_dtini = (  select max(ver_dtini) 
                                 from v_cufot 
                                 where az_id = cu.az_id
                                 and cuf_id = cu.cuf_id 
                                 and ctu_numcom = cu.ctu_numcom
                                 and sta_idsys =  cu.sta_idsys
                                 and ver_dtini <= fsez.sfat_dtfinevalcon
                                 and ver_dtfine >= fsez.sfat_dtinivalcon  )
           and o.az_id = cu.az_id
           and o.ot_id = cu.ot_id
           and o.ser_id = '03'
           and o.catcomser_id = 10
           and o.comser_id = 27
            )                                                        OPZIONE_TRASPORTOVENDITA,           
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '251' )         IMP_QUOTA_FISSA_DISTRIB,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '250' )         IMP_QUOTA_ENERGIA_DISTRIB,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '252' )         IMP_QUOTA_POTENZA_DISTRIB,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '248,283' )     IMP_A6_QUOTA_POTENZA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '312' )         IMP_QUOTA_FISSA_DOMESTICO,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '311' )         IMP_QUOTA_ENERGIA_DOMESTICO,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '313' )         IMP_QUOTA_POTENZA_DOMESTICO,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '342' )         IMP_SCONTO_PREZ_VEND_DOMESTICO,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '281' )                  IMP_TRASMISSIONE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '303' )                  IMP_MISURA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '302,299,300' )          IMP_REATTIVA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '289,290,291,297' )      IMP_COMPONENTI_A_FISSE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '284,285,282,286,287,288,298' )   IMP_COMPONENTI_A,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '295' )               IMP_COMPONENTI_UC_FISSE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '292,293,294' )       IMP_COMPONENTI_UC,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '296' )               IMP_COMPONENTI_MCT,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '234,207,208,209' )   IMP_PREZZO_VENDITA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '217,240,216,229' )   IMP_PERDITE_VENDITA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '225,226,227,228' )   IMP_SCONTO_VENDITA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '205' )               IMP_SBILANCIAMENTO_VENDITA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '236,237,238,239' )   IMP_DISPACCIAMENTO_VENDITA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '232' )               IMP_UC1_VENDITA,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '220' )               IMP_PCV,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '221' )               IMP_PPE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '242' )               IMP_IMPOSTA_ERARIALE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '244' )               IMP_IMPOSTA_ADDIZ_PROV,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '245' )               IMP_IMPOSTA_ADDIZ_COMUNALE,
        BO_FATTURAR.TOTALE_RIGA_VOCE( ft.az_id, ft.tfat_id, fsez.sfat_id, '402' )               CTS,

        ( select sum(fatvoceot_imp) 
          from v_fatturar
          where az_id = ft.az_id 
          and tfat_id = ft.tfat_id
          and ot_id = 0 
          and voce_id  = 83 )                                             TOT_ADD_DA_BOLL_PREC_0_5,                        
        ( select sum(fatvoceot_imp) 
          from v_fatturar
          where az_id = ft.az_id 
          and   tfat_id = ft.tfat_id
          and ot_id = 0 
          and voce_id = 84 )                                             TOT_ACC_DA_BOLL_PREC_ANT,                        
        ( select sum(fatvoceot_imp) 
          from v_fatturar
          where az_id = ft.az_id 
          and   tfat_id = ft.tfat_id
          and ot_id = 0 
          and voce_id  = 46 )                                                BOLLO_QUIETANZA,                        
        ( select sum(pfat_imp) 
         from fatturap 
         where tfat_id = ft.tfat_id
         and cuf_id = cuf.cuf_id )   TOT_IMPONIBILE_XCUF,
         ( select sum(pfat_iva) 
             from fatturap
            where tfat_id = ft.tfat_id
            and cuf_id = cuf.cuf_id )   TOT_IVA_XCUF,
         ( select sum(pfat_tot)
            from fatturap
            where tfat_id = ft.tfat_id
            and cuf_id = cuf.cuf_id )   TOT_FATTURA_XCUF,
      BO_FATTURAT.TOTALE_IMPONIBILE( ft.az_id, ft.tfat_id )                             TOT_IMPONIBILE_FATTURA,
      BO_FATTURAT.TOTALE_IVA( ft.az_id, ft.tfat_id )                                    TOT_IVA_FATTURA,        
      BO_FATTURAT.TOTALE_DA_PAGARE( ft.az_id, ft.tfat_id )                              TOT_FATTURA,            
      decode ( RFRP_FLTIPOPAG, 'D','RID - Domiciliazione Bancaria','B','Bonifico Bancario','R','RimessaDiretta','P','BollettinoPostale') TIPO_PAGAMENTO,
      banapp_abi Banca_appoggio,   
      decode ( RFRP_FLSTABOL, 0, 'No', 1, 'Si' )   STAMPA_BOLLETTINO_POSTALE,

      ( select sum(  r.RATA_IMP )
         from rata   r, dilazione d
         where   d.az_id   = ft.az_id
            and   d.par_id   = ft.par_id 
            and d.ver_num = ( select max(ver_num) from v_dilazione where az_id = d.az_id and par_id = d.par_id)
            and r.az_id = d.az_id
            and r.dil_vernum = d.ver_num
            and r.rata_num = 1  )       IMP_RATA_1,
      ( select to_char(r.RATA_DTSCA,'dd/mm/yyyy') 
         from rata   r, dilazione d
         where   d.az_id   = ft.az_id
            and d.par_id   = ft.par_id 
            and d.ver_num = ( select max(ver_num) from v_dilazione where az_id = d.az_id and par_id = d.par_id)
            and r.az_id = d.az_id
            and r.dil_vernum = d.ver_num
            and r.rata_num = 1  )       DTSCA_RATA_1
          
from  v_fatturat        ft,
      ragfat            rf,
      v_ragfatgrufat    rg,      
      soggetto          sog,
      v_conubifrn       cuf,
      contratto         cont,
      fatturas          fsez
where ft.az_id       = '01'
and cuf.SER_ID='03'

and    ft.ftz_num    >=   ^Numero Fatturazione da;N;Numerico^^
and    ft.ftz_num    <= ^Numero Fatturazione  a;N;Numerico^^

and   rf.az_id       = ft.az_id
and   rf.ragf_id     = ft.ragf_id
and   rg.az_id    = ft.az_id
and   rg.ragf_id  = ft.ragf_id
and   rg.STA_IDSYS = 2
and   rg.sta_idsys(+) <> 3
and   sog.az_id      = ft.az_id
and   sog.sog_id     = rf.sog_id
and   fsez.az_id     = ft.az_id
and   fsez.tfat_id   = ft.tfat_id
and   cuf.az_id      = fsez.az_id(+)
and   cuf.cuf_id     = fsez.cuf_id(+)
and   cont.az_id     = ft.az_id
and   cont.con_anno  = cuf.con_anno
and   cont.con_id    = cuf.con_id
--and   cont.con_anno  = 2009
--and   cont.con_id    = 16607

--and ft.ftz_num =12483 
order by fat_anno, fat_num, cuf.con_anno, cuf.con_id
